<?php

namespace App\Service\User\v1_1_4;

use App\Constants\ErrorCode;
use App\Model\Act\ProfileRewardLog;
use App\Model\ProfileVerifyLog;
use App\Model\Service\NewGirlReplyPackageService;
use App\Model\UserProfile;
use App\Model\UserTag;
use App\Service\BaseService;

/**
 * 用户资料编辑
 */
class ProfileExtraModifyService extends BaseService
{
    //修改字段
    public $modifyFields = [
        'poster_video', //照片墙视频
        'poster_video_duration', //照片墙视频时长
        'poster', //照片墙图片
        'tags',   //标签列表
        'love_tags', //喜欢的标签
    ];

    public function handle()
    {
        $userId = $this->getUserId();
        $allPost = $this->request->all();
        if (
            !isset($allPost['poster'])
            && !isset($allPost['poster_video'])
            && !isset($allPost['tags'])
            && !isset($allPost['love_tags'])
        ) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '参数不完整！');
        }

        if (isset($allPost['poster']) && $allPost['poster']) {
            $posters = explode(',', $allPost['poster']);
            foreach ($posters as $poster) {
                if (!$poster) return $this->setError(ErrorCode::PARAMETER_ERROR, '提交图片参数不完整！');
            }
        }

        //提交标签
        if (isset($allPost['tags']) && $allPost['tags']) {
            $tags = explode(',', $allPost['tags']);
            if (count($tags) > config('busi.user_tag_max')) return $this->setError(ErrorCode::PARAMETER_ERROR, '最多只可选择10个标签哦～');
        }

        //更新照片墙
        $result = $this->updateInfo($userId, $allPost);

        return ['result' => $result ? 1 : 0];
    }

    /**
     * 过滤更新资料的字段信息
     */
    function updateInfo($userId = 0, $allPost = [])
    {
        $oldProfile = UserProfile::getProfile($userId);
        foreach ($this->modifyFields as $field) {
            if (!isset($allPost[$field])) continue;
            // log_info(__METHOD__ . "=={$userId}=={$field}==" . json_encode($allPost));
            switch ($field) {
                    //照片
                case 'poster':
                    $posters = explode(',', $allPost['poster']);
                    $profile = UserProfile::getProfile($userId);
                    $oldPosters = $profile->poster ? explode(',', $profile->poster) : [];
                    $oldAllPosters = array_slice(array_unique(array_merge(
                        $oldPosters,
                        ProfileVerifyLog::getVerfingPosters($userId, 5)
                    )), 0, 5);
                    $newPoster = array_values(array_diff($posters, $oldAllPosters));
                    log_info(__METHOD__ . "=={$userId}==NewPoster:" . json_encode($newPoster));

                    $posterStatus = check_dev_env() ? ProfileVerifyLog::STATUS_ING : ProfileVerifyLog::STATUS_OK;
                    if ($posterStatus == ProfileVerifyLog::STATUS_OK) {
                        UserProfile::updateInfo($userId, ['poster' => $allPost['poster']]);
                    }
                    //审核记录
                    $newPoster && $newPoster[0] && ProfileVerifyLog::addRecord(
                        $userId,
                        ProfileVerifyLog::TYPE_POSTER,
                        $newPoster[0],
                        $posterStatus
                    );
                    //奖励
                    !$oldProfile->poster && ProfileRewardLog::doReward($userId, 'poster', 2);
                    break;

                    //视频介绍
                case 'poster_video':
                    $videoInfo = $allPost['poster_video'] ? json_encode([
                        'link' => $allPost['poster_video'],
                        'duration' => $allPost['poster_video_duration'] ?? 0,
                    ]) : '';
                    UserProfile::updateInfo($userId, ['video' => $videoInfo]);
                    $allPost['poster_video'] && ProfileVerifyLog::addRecord($userId, ProfileVerifyLog::TYPE_VIDEO, $allPost['poster_video']);
                    //奖励
                    !$oldProfile->poster_video && ProfileRewardLog::doReward($userId, 'poster', 2);
                    break;

                    //标签
                case 'tags':
                    if (!$allPost['tags']) UserTag::clearTags($userId);
                    else {
                        $tags = explode(',', $allPost['tags']);
                        UserTag::updateTags($userId, $tags);
                    }
                    ProfileRewardLog::doReward($userId, 'tags', 2);
                    break;

                    //喜欢的标签
                case 'love_tags':
                    if (!$allPost['love_tags']) UserTag::clearTags($userId, UserTag::TYPE_LOVE);
                    else {
                        $tags = explode(',', $allPost['love_tags']);
                        UserTag::updateTags($userId, $tags, UserTag::TYPE_LOVE);
                    }
                    break;
            }
        }

        $newGirlTaskValid = config('act.new_girl_task_valid', '2023-03-12');
        if ($oldProfile->gender == 0 && $oldProfile->created_at >= $newGirlTaskValid) {
            NewGirlReplyPackageService::checkFinishUserInfo((int)$userId);
        }

        return 1;
    }
}
